﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace eStore.UserControl
{
    public partial class LoginUI : System.Web.UI.UserControl
    {
        private const string STR_LoginUI = "LoginUI";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            	 Page_LoadResources();
            }
           
        }
        private void Page_LoadResources()
        {
            LegendLogin.InnerText = (string)GetGlobalResourceObject(STR_LoginUI, "LegendLogin");
            h2HeaderLogin.InnerText = (string)GetGlobalResourceObject(STR_LoginUI, "h2HeaderLogin");
            pLoginInfo.InnerText = (string)GetGlobalResourceObject(STR_LoginUI, "pLoginInfo");
            //ValidationSummaryLogin.HeaderText = (string)GetGlobalResourceObject(STR_LoginUI, "ValidationSummaryLogin");
            LabelUserName.Text = (string)GetGlobalResourceObject(STR_LoginUI, "LabelUserName");
            LabelPassword.Text = (string)GetGlobalResourceObject(STR_LoginUI, "LabelPassword");
            RequiredFieldValidatorUserName.ErrorMessage = (string)GetGlobalResourceObject(STR_LoginUI, "RequiredFieldValidatorUserName");
            RequiredFieldValidatorPassword.ErrorMessage = (string)GetGlobalResourceObject(STR_LoginUI, "RequiredFieldValidatorPassword");
            ButtonLogin.Text = (string)GetGlobalResourceObject(STR_LoginUI, "ButtonLogin");
        }

        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                bool bValidate=Membership.ValidateUser(TextBoxUserName.Text, TextBoxPassword.Text);
               
                if (bValidate)
                {
                    FormsAuthentication.SetAuthCookie(TextBoxUserName.Text, true);
                    Session["UserName"] = TextBoxUserName.Text;
                    Response.Redirect("~/Home");
                }
                else
                {
                    Server.Transfer("~/Info/Message.aspx?title=" + Page.Title + "&&langstore=" + STR_LoginUI + "&&header=h2HeaderLogin" + "&&Message=MessageError&&DetailsHeader=DetailsHeaderError&&DetailsText=DetailsTextError&&btnBack=btnBackError&&urlBack=" + Request.Url.ToString());
                }
                
            }
        }
    }
}